<?php 
session_start();
ob_start();
require_once("../../includes/common.php");
getConnection();
$action=$_REQUEST['action'];
switch($action){
		case "checkForUniqueCodeForEntity":
		$code=$_GET['code'];
		$row=Entity::checkForUniqueCodeNo($code);
		$val= isset($row[0]) ? '0' : '1';
		$resoponse=["code"=>$code, "result"=>$val];
		echo json_encode($resoponse);
	break;
	
		case "checkForUniqueEmail":
		$emailId=$_GET['emailId'];
		$entityId=$_GET['entityId'];
		$row=Users::checkForUniqueEmail($emailId,$entityId);
		$val= isset($row[0]) ? '0' : '1';
		echo $val;
	break;
	
		case "passwordResetEmail":
		$userId=$_REQUEST["id"];
		$dT=date("Y-m-d H:i:s");
		$code=md5(rand(100,1000)).md5($userId).md5($dT);
		$url="http://".BASE_DIR."/passwordreset.php?code=".$code."&userId=".$userId;
		$sql= " 
				UPDATE  `users` 
				SET  
				`passwordReset` =  '{$code}' 
				WHERE  `id` ={$userId};
			";
		$result=executeUpdateQuery($sql);
		$r=["status"=>"error","msg"=>"There was some problem, please try again"];
		if(isset($result) && $result>0){
			$email=Users::getUserEmailFromId($userId);
			$html=getPasswordResetEmailFormat($url);
			//setting up params for email 
			$params=[
						"emails"=>$email,
						"header"=>"Enview-Motor Tool : Password Reset",
						"html"=>$html,
						"emailClassification"=>"individualEmail",
						"subject"=>"Enview-Motor Tool : Password Reset",
					];
			$em=new EmailAPI($params);
			if($em->send())
				$r=["status"=>"success","msg"=>"Password Reset Email has been sent"];
		}
		echo json_encode($r);	
	break;
	case "fillAmountSplits":
		$mem_id=$_GET["mem_id"];
		$month_year=date("Ym");
		echo json_encode(Members::calculateAmountPayable($mem_id,$month_year));
	break;
	case "refreshDueChart":
		$date=isset($_REQUEST["date"]) ? $_REQUEST["date"] : date("Y-m-d");
		$id=isset($_REQUEST['id']) ? [$_REQUEST['id']] : NULL;
		if($id==NULL){
			$userList=Members::getMemberList();
			foreach($userList as $users){
				$id[]=$users['id'];
			}
		}
		foreach($id as $v){
			AdminActivities::refreshPayment($v,$date);
		}
		echo "success";
	break;
	
	
	} // end of switch

ob_end_flush();


?>